package likou;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/10/29 08:59
 */
public class _335 {
    class Path {
        int start;
        int end;// start<end

        Path(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    public boolean isSelfCrossing(int[] distance) {
        Map<Integer, List<Path>> rowMap = new HashMap<>();
        Map<Integer, List<Path>> colMap = new HashMap<>();
        int[] point = new int[2];
        for (int i = 0; i < distance.length; i++) {
            if (i % 4 == 0) {
                //向上
                if (zhuangxian(rowMap, point[1], point[1] + distance[i], point[0])) {
                    return true;
                }
                if (zhuangxian(colMap, point[1], point[1] + distance[i], point[0])) {
                    return true;
                }
                put(colMap, point[1], point[1] + distance[i], point[0]);
                point[1] += distance[i];
            } else if (i % 4 == 1) {
                //向左
                if (zhuangxian(colMap, point[0], point[0] - distance[i], point[1])) {
                    return true;
                }
                if (zhuangxian(rowMap, point[0], point[0] - distance[i], point[1])) {
                    return true;
                }
                put(rowMap, point[0], point[0] - distance[i], point[1]);
                point[0] -= distance[i];
            } else if (i % 4 == 2) {
                //向下
                if (zhuangxian(rowMap, point[1], point[1] - distance[i], point[0])) {
                    return true;
                }
                if (zhuangxian(colMap, point[1], point[1] - distance[i], point[0])) {
                    return true;
                }
                put(colMap, point[1], point[1] - distance[i], point[0]);
                point[1] -= distance[i];
            } else {
                //向右
                if (zhuangxian(colMap, point[0], point[0] + distance[i], point[1])) {
                    return true;
                }
                if (zhuangxian(rowMap, point[0], point[0] + distance[i], point[1])) {
                    return true;
                }
                put(rowMap, point[0], point[0] + distance[i], point[1]);
                point[0] += distance[i];
            }
        }
        return false;
    }

    // start<=key<=end：可以穿过，value[0]<=point[七点]<=value[1]
    public boolean zhuangxian(Map<Integer, List<Path>> map, int start, int end, int point) {
        for (int key : map.keySet()) {
            if (start <= key && key <= end) {
                List<Path> pathList = map.get(key);
                for (Path path : pathList) {
                    int min = Math.min(path.start, path.end);
                    int max = Math.max(path.start, path.end);
                    if (path.end != point || key != start) {
                        if (min <= point && point <= max) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    public void put(Map<Integer, List<Path>> map, int start, int end, int point) {
        if (map.containsKey(point)) {
            map.get(point).add(new Path(start, end));
        } else {
            map.put(point, new ArrayList<Path>() {{
                add(new Path(start, end));
            }});
        }
    }

    public static void main(String[] args) {
        _335 o = new _335();
        System.out.println(o.isSelfCrossing(new int[]{1, 1, 2, 1, 1}));
    }
}
